package com.ovopark.firecar.utils

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.ovopark.firecar.R

/**
 * Created by cz on 2018/3/21.
 */
class NotificationUtils(context: Context?) : ContextWrapper(context) {
    private var mNotifyManager: NotificationManager? = null
    private var lowBuilder: NotificationCompat.Builder? = null
    private var oBuilder: Notification.Builder? = null
    fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW)
            channel.enableVibration(false)
            channel.vibrationPattern = longArrayOf(0)
            channel.enableLights(false)
            manager!!.createNotificationChannel(channel)
        }
    }

    val manager: NotificationManager?
        get() {
            if (mNotifyManager == null) {
                mNotifyManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            }
            return mNotifyManager
        }

    @RequiresApi(api = Build.VERSION_CODES.O)
    fun getChannelNotification(title: String?, content: String?): Notification.Builder {
        return Notification.Builder(applicationContext, id)
            .setContentTitle(title)
            .setContentText(content)
            .setSmallIcon(R.drawable.ic_log)
            .setAutoCancel(true)
    }

    fun getNotification_25(title: String?, content: String?): NotificationCompat.Builder {
        return NotificationCompat.Builder(applicationContext)
            .setContentTitle(title)
            .setContentText(content)
            .setSmallIcon(R.drawable.ic_log)
            .setAutoCancel(true)
    }

    fun sendNotification(title: String?, content: String?) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel()
            val notification = getChannelNotification(title, content).build()
            manager!!.notify(1, notification)
        } else {
            val notification = getNotification_25(title, content).build()
            manager!!.notify(1, notification)
        }
    }

    fun initNotification(title: String?, ticker: String?) {
        val icon = applicationInfo.icon
        manager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            oBuilder = Notification.Builder(applicationContext, id)
                .setContentTitle(title)
                .setSmallIcon(icon)
                .setTicker(ticker)
                .setAutoCancel(true)
        } else {
            lowBuilder = NotificationCompat.Builder(applicationContext)
                .setContentTitle(title)
                .setSmallIcon(icon)
                .setTicker(getString(R.string.update_download_progress))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true)
        }
    }

    fun startNotification(title: String?, content: String?): Notification {
        val notification: Notification
        notification = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel()
            getChannelNotification(title, content).build()
        } else {
            getNotification_25(title, content).build()
        }
        return notification
    }

    fun getNotification(progress: Int): Notification {
        val pendingintent =
            PendingIntent.getActivity(this, 0, Intent(), PendingIntent.FLAG_CANCEL_CURRENT)
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            oBuilder!!.setContentText(
                this.getString(
                    R.string.update_download_progress,
                    progress
                )
            )
                .setProgress(100, progress, false)
            oBuilder!!.setContentIntent(pendingintent)
            oBuilder!!.build()
        } else {
            lowBuilder!!.setContentText(
                this.getString(
                    R.string.update_download_progress,
                    progress
                )
            )
                .setProgress(100, progress, false)
            lowBuilder!!.setContentIntent(pendingintent)
            lowBuilder!!.build()
        }
    }

    companion object {
        const val id = "ovopark"
        const val name = "wandianzhang"
    }
}